Welcome back. So, we've already gone over the accumulation pattern,
where you iterate over a sequence and update
an accumulator variable as you iterate through every item in that sequence.
In this lesson, we're going to go over the dictionary accumulation pattern,
which is the same idea except our accumulator variable is
going to be a dictionary that has multiple key value pairs.
So, let's start out with the standard accumulation pattern example.
So, on one here,
we open a file named scarlet.txt and we open it to read it.
Then we say, text is f.read.
So, in other words, txt is a variable which is
a string and it's the contents of the file scarlet.txt.
So, suppose scarlet.txt represents the text of the book, The Scarlet Letter.
So, let's say that we want to keep track of how many t's are in scarlet.txt.
The way that we do that, is with the accumulation pattern.
So, here, t_count is our accumulator variable.
We initialize it to be 0 at first,
to say that we've seen 0 t's so far,
and then we iterate through every character in our text file and we say,
if that character is the letter t,
and then say, t_count equals t_count plus one.
In other words, we just saw one more character t. So,
by the time we're on line eight and we're done with our for loop,
if we print out the value of t_count,
then we should have the number of t's in scarlet.txt.
So, if I run my code,
then I'll see that here,
there are 17,584 occurrences of the character t in scarlet.txt.
So, that's great, that's the standard accumulation pattern,
but let's suppose that we wanted to count more letters than just the letter t. So,
suppose that we also wanted to keep track of how many s's are in scarlet.txt?
Well, we could do that in almost the same way.
So, here, we open up scarlet.txt again,
and then we read it,
except now on lines four and five,
we create two different accumulator variables.
We have t_count to keep track of the number of t's and we initialize that to
zero and then we have s_count to keep track of the number of s's.
Like before, we still iterate over every character in txt,
but now we say, if that character is a t,
then t-count is t_count plus one.
If the character is an s,
then we instead update s_count.
So, by the end of our for loop,
t_count is going to be the number of t's and s_count is going to be the number of s's.
So, you could imagine doing this for any number of characters,
but for every character that we would want to accumulate,
we would have to create a new accumulator variable here.
So, we might have a_count,
then b_count, then c_count and so on.
You can imagine that if we wanted to count every character in the alphabet,
initializing 26 accumulated variables might be just a little bit lengthy codewise.
So, one alternative way to do this,
and it's going to seem a little bit weird at first,
but I'm going to get to why we want to do this,
is by instead using a dictionary.
So, like the code before,
we open up the files scarlet.txt,
and then we read it in.
Now, I'm going to have one accumulator variable which is a dictionary.
So, I say, x equals an empty dictionary.
Inside of that dictionary,
we're going to have multiple key value pairs.
So, if we wanted to still only count t's and s's,
then rather than saying t_count equals zero,
I'm going to say x sub t equals zero,
and rather than saying s_count equals zero,
I'm going to say x sub s equals zero.
Now, these are just different key value pairs in this same dictionary x.
So, now what we can do,
is we can loop through every character in our file once again,
and again we say,
if that character is a t,
then our dictionary x sub t equals its previous value plus one.
If that character is an s,
then x sub s gets incremented by one instead.
Again, by the time our for loop is done,
then we're going to have x sub t as the number of t's in our dictionary,
and x sub s is going to be the number of s's in scarlet.txt.
So, when we run our code,
then we can see the number of t's and the number of s's.
So, now, I'm going to make one really small change to our code.
So, here, on line nine,
this statement is inside of if c equals the character t. So,
what we can do is,
we can replace x sub t here.
So, we can replace the hard coded t with the variable c. We know that this is going to be
the same because here we only run this code if c is the character t. So,
we can say, x sub c equals x sub c plus one, and here,
we can say x sub c equals x sub c plus one,
because this line is inside of this elif c equals equals the character s. So,
in other words, what we're going to just do in the next piece of code is,
we're going to replace the hard coded s and
hard coded t with the value of the variable c. So,
if we do that, we get something that looks like this.
So, we say, if the character c is t,
then say x sub c equals it,
s previous value plus one.
Now, because again, this is inside of an if statement,
we know that c is going to be t here,
but we'll get to why we actually want to make this change in a little bit.
Same thing with this elif.
So, we know that t is going to be the character s here,
but we just replaced the hard coded s with
the value of the variable c. If we run our code,
we're going to get the exact same result as before.
So, now I want to go into why we actually wanted to replace that
hard coded t and hard-coded s with the value of the variable c. So,
let's suppose that rather than just counting the number of
t's and the number of s's in scarlet.txt,
we wanted to count the number of every single character.
So, the number of a's, b's,
c's, s's and t's,
spaces, exclamation points and so on.
So, we could do that by replacing line four with a whole bunch of accumulated variables.
So, a_count, b_count, c_count, exclamation point_count, etc.,
but then our code would get really long
and really repetitive because we would need to initialize
a separate accumulator variable for every single character that might be in scarlet.txt.
Instead, what we're going to do is,
the dictionary accumulation pattern.
So, we're going to have one accumulator variable which is a dictionary.
So, on line four,
we say, x equals an empty dictionary.
Then like before, we loop through every character in txt and what we do is,
we have an if statement to say,
if the character c is not in our accumulator dictionary.
So, if c is not an x.
So, in other words, if we haven't encountered this new character c yet,
then we initialize x sub c to be zero.
What that means is that,
the first time we see the character a,
then we're going to initialize x sub a to be zero.
The first time we see the character t,
then we initialize x sub t to be zero and so on.
Now, here on line 11,
still inside of this for loop,
we say x sub c equals x sub c plus one.
In other words, we add one to it's previous value for whatever this character c is.
So, in other words,
if the character c is the letter a,
then we say x sub a equals x sub a plus one,
and that says that we saw one more a.
If the character is the letter t,
then we say x sub t equals it's previous value plus one and so on.
So, here on line 13,
we just print out the number of characters t and the number of characters s on line 14.
So, what we should find is that we actually get
the exact same value for t and s as before.
So, if I run my code,
then we will see that we get the correct number of t's and s's,
but what's great here is that we have more than just t's and s's collected.
We can print out the number of any character we want.
So, I can print out the number of a's just by saying,
number of a's is x sub a and the number of b's is x sub b.
You'll see that our dictionary is keeping track of
every single letter that might occur in scarlet.txt.
So, to illustrate how this works,
I'm going to go with just a slightly simpler example.
So, rather than assigning txt to be the value in scarlet.txt,
I'm just going to say,
txt equals the string MICHIGAN and I am going to leave the rest of the code the same,
except, I'm going to come out lines 12 through 15.
Now, I'm going to run my code in code lens.
So, you can see line one initializes txt to be MICHIGAN.
Line three initializes our accumulator variable x to start out as an empty dictionary,
and then we're going to loop through every character inside of our string MICHIGAN.
So, c is first assigned to the character M because that's the first character,
and we say, is c in our dictionary already?
In our case, it's not.
So, we assign x sub c or x sub M to be zero.
Now on line 10, we immediately increment the value associated with x sub M. So,
we say, we've seen one M so far.
With the character I, then we see that I is not in our dictionary.
So, we initialize x sub I to be
zero and we set it's value to it's previous value plus one.
With, it's not in our dictionary,
so we initialize it to zero and then increment it.
With H , we set it to zero and then increment it.
So, you can see that our dictionary keeps building
up the number of times that we've seen every given letter.
So, at this point, we've seen M,
I, C H and so far,
none of these characters were in our dictionary and so our dictionary was
just adding a new key value pair for every letter that we saw.
Now, the next character is going to be this letter I.
So, you can see, c is the character I.
Now, the key point here is that,
I is already in our dictionary.
So, here, this if is not going to execute,
because I is in our dictionary, and so,
rather than setting x sub i to be zero,
we just increment it on line 10.
So, you'll see, x sub I go from one to two,
to say that we've seen two I's so far.
In the case of g, it's not in our dictionary,
so we add a new key value pair and increment it's value and so on.
So, by the time our for loop is done,
then we end up with the dictionary,
where every key is a character in our text txt
and every value is the number of times that we've actually seen that letter.
So, we'll see dictionary accumulation a lot and
really I find that it just takes a lot of practice to get used to.
It's a little bit counterintuitive at first,
but we're going to go through more examples with
dictionary accumulation and I think it's
going to make more and more sense with more practice.
That's all for now, until next time.